package frame;

import clsaa.Biglotto;
import data.ConfigBigLotto;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class chessoOneFrame extends JFrame{

    static JPanel dataShow;
    private JTextField frontArea1Field;
    private JTextField frontArea2Field;
    private JTextField frontArea3Field;
    private JTextField frontArea4Field;
    private JTextField frontArea5Field;
    private JTextField backArea1Field;
    private JTextField backArea2Field;

    public int sssss(int n){
        if(n<0)return 0;
        else return n;
    }
    public void initdataShow(){
        if(dataShow != null)dataShow.removeAll();
        dataShow.setBounds(0,50,400,350);
        // 创建一个滚动面板
        JScrollPane scrollPane = new JScrollPane();

        // 创建一个JTextArea用于显示文本
        JTextArea textArea = new JTextArea();
        textArea.setEditable(false); // 设置文本区域为不可编辑
        textArea.setLineWrap(true); // 设置自动换行
        textArea.setWrapStyleWord(true); // 设置在单词边界处换行
        // 将JTextArea添加到滚动面板中
        scrollPane.setViewportView(textArea);

        int[] dataA=new int[5];
        int[] dataB=new int[2];
        Random rand = new Random();
        for(int i=0;i<5;){
            int data=sssss(rand.nextInt())%35 +1;
            int v=0;
            for(int j=0;j<i;++j){
                if(dataA[j]==data){
                    v=1;
                    break;
                }
            }
            if(v==0){
                dataA[i++]=data;
            }
        }
        for(int i=0;i<4;++i){
            for(int j=i+1;j<5;++j){
                if(dataA[i]>dataA[j]){
                    int v=dataA[i];
                    dataA[i]=dataA[j];
                    dataA[j]=v;
                }
            }
        }
        for(int i=0;i<2;){
            int data=sssss(rand.nextInt())%35 +1;
            int v=0;
            for(int j=0;j<i;++j){
                if(dataB[j]==data){
                    v=1;
                    break;
                }
            }
            if(v==0){
                dataB[i++]=data;
            }
        }
        if(dataB[0]>dataB[1]){
            int v=dataB[0];
            dataB[0]=dataB[1];
            dataB[1]=v;
        }
        textArea.removeAll();
        textArea.append("随机生成一注："+dataA[0]+"  "+dataA[1]+"  "+dataA[2]+"  "+dataA[3]+"  "+dataA[4]+" + "+dataB[0]+"  "+dataB[1]+"\n");
        textArea.append("和以往进行对比：\n");

        ConfigBigLotto configBigLotto=new ConfigBigLotto();
        configBigLotto.getAllbiglotto().reorderAscending();
        int[] zhongjiang=new int[]{0,0,0,0,0,0,0,0,0};
        for(Biglotto biglotto:configBigLotto.getAllbiglotto().getAllData()){
            int what=biglotto.duibi(dataA[0],dataA[1],dataA[2],dataA[3],dataA[4],dataB[0],dataB[1]);
            if(what!=0){
                textArea.append("找到"+what+"等奖:"+biglotto.ThistoString());
                zhongjiang[what-1]++;
            }

        }
        textArea.append("查找数据为："+dataA[0]+"  "+dataA[1]+"  "+dataA[2]+"  "+dataA[3]+"  "+dataA[4]+" + "+dataB[0]+"  "+dataB[1]+" ...... \n");
        textArea.append("在"+configBigLotto.getAllbiglotto().getAllData().size()+"条历史数据中寻找到：\n");
        textArea.append("一等奖："+zhongjiang[0]+"注  二等奖："+zhongjiang[1]+"注  三等奖："+zhongjiang[2]+"注  \n四等奖："+zhongjiang[3]+"注  五等奖："+zhongjiang[4]+"注  六等奖："+zhongjiang[5]+"注  \n七等奖："+zhongjiang[6]+"注  八等奖："+zhongjiang[7]+"注  九等奖："+zhongjiang[8]+"注\n");

        // 创建一个主面板并使用BorderLayout
        dataShow.add(scrollPane, BorderLayout.CENTER);
        add(dataShow);

        dataShow.updateUI();
    }

    public chessoOneFrame() {
        super("随机机选一注");

        // 设置窗口关闭操作
        setDefaultCloseOperation(2);
        // 设置窗口大小
        setSize(400, 450);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        // 计算JFrame的左上角应该放在屏幕上的位置
        int x = (screenSize.width - this.getWidth()) / 2;
        int y = (screenSize.height - this.getHeight()) / 2;

        // 设置JFrame的位置
        this.setLocation(x, y);
        setLayout(null);


        JButton JBfin=new JButton("随机一注");
        JBfin.setBounds(100,0,100,50);
        JBfin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 取消按钮的逻辑（例如关闭窗口）
                initdataShow();
            }
        });
        add(JBfin);

        dataShow=new JPanel(new BorderLayout());
        initdataShow();
        add(dataShow);
        setVisible(true);
        new Thread(new chessoOneFrame.NoWeightedGoods()).start();
    }

    public static void main(String[] args) {
        // 创建并显示窗口
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new chessoOneFrame();
            }
        });
    }
    public class NoWeightedGoods implements Runnable{
        @Override
        public void run() {
            while(true){
                repaint();
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
